package com.example.elderlycare.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.elderlycare.controller.entity.RetardRecordRequestBody;
import com.example.elderlycare.dao.entity.Berth;
import com.example.elderlycare.dao.entity.CheckRecord;
import com.example.elderlycare.dao.entity.RetardRecord;
import com.example.elderlycare.service.BerthService;
import com.example.elderlycare.service.CheckRecordService;
import com.example.elderlycare.service.RetardRecordService;
import com.example.elderlycare.service.entity.RetardRecordListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/retard_record")
public class RetardRecordController {

    @Resource
    RetardRecordService retardRecordService;

    @Resource
    CheckRecordService checkRecordService;

    @Resource
    BerthService berthService;

    /**
     * 查询退住登记记录的接口
     *
     * @param elderName 老人的姓名
     * @param startDate 出院日期筛选的开始日期
     * @param endDate   出院日期筛选的开始日期
     * @param pageSize  页大小
     * @param current   当前页
     * @return RetardRecordListResponseBody
     */
    @RequestMapping("/query")
    public Result<RetardRecordListResponseBody> queryRetardRecords(
            @RequestParam(value = "elderName", required = false) String elderName,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("elderName", elderName != null ? "%" + elderName + "%" : null);
        params.put("limit", pageSize);
        params.put("offset", (current - 1) * pageSize);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        try {
            RetardRecordListResponseBody res = retardRecordService.queryRetardRecords(params);
            return Result.success(res);
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 新增退住登记记录的接口
     *
     * @param req RetardRecordRequestBody.addRetardRecord
     * @return null
     */
    @PostMapping("/add")
    public Result<?> addRetardRecord(@RequestBody RetardRecordRequestBody.addRetardRecord req) {
        Integer checkRecordId = req.getCheckRecordId();
        CheckRecord checkRecord = checkRecordService.getById(checkRecordId);
        RetardRecord record = RetardRecord.builder()
                .retardDate(req.getRetardDate())
                .evaluation(req.getEvaluation())
                .reason(req.getReason())
                .elderId(checkRecord.getElderId())
                .build();
        try {
            boolean flag = retardRecordService.save(record);
            if (flag) {
                /* 退住后释放原来的床位 */
                UpdateWrapper<Berth> wrapper = new UpdateWrapper<>();
                wrapper.eq("id", checkRecord.getBedId());
                wrapper.set("elder_id", null);
                berthService.update(wrapper);
                /* 更新入住状态为已退住 */
                checkRecord.setInStatusId(112);
                checkRecordService.updateById(checkRecord);
                return Result.success(null);
            } else {
                log.warn("add RetardRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 修改退住登记的参数
     *
     * @param req RetardRecordRequestBody.updateRetardRecord
     * @return null
     */
    @PutMapping("/update")
    public Result<?> updateRetardRecord(@RequestBody RetardRecordRequestBody.updateRetardRecord req) {
        RetardRecord record = RetardRecord.builder()
                .id(req.getId())
                .retardDate(req.getRetardDate())
                .evaluation(req.getEvaluation())
                .reason(req.getReason())
                .elderId(req.getElderId())
                .build();
        try {
            boolean flag = retardRecordService.updateById(record);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("update RetardRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 删除退住登记记录的接口
     *
     * @param id 退住登记记录的id
     * @return null
     */
    @DeleteMapping("/delete")
    public Result<?> deleteRetardRecord(@RequestParam("id") @NotEmpty Integer id) {
        try {
            boolean flag = retardRecordService.removeById(id);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("delete RetardRecord error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }
}
